<?php
/**
 * Created by jinqiang on 2024/7/11 13:25.
 * Copyright (c) 2024, Your Company. All rights reserved.
 */


namespace App\Http\Services\Produce;


use App\Http\Models\Mysql\SsyProcessingListDetailsTable;
use App\Http\Models\Mysql\SsyProcessingListTable;
use App\Http\Models\Mysql\SsyProductlistBangdinNewTable;
use Illuminate\Support\Facades\DB;
use function Symfony\Component\Translation\t;

class LiningService
{
    public function search($param)
    {
        $conn = new SsyProductlistBangdinNewTable();
        if($param['type_no'] ?? ""){
            $conn = $conn->where('type_no', 'like', $param['type_no']."%");
        }
        if($param['sku_code'] ?? ""){
            $conn = $conn->where('sku_code', 'like', $param['sku_code']."%");
        }
        $res = $conn->orderBy('createtime', 'desc')->paginate((Int)$param['limit'])->toArray();
        foreach($res['data'] as &$item){
            $item['ximaiimg'] = unserialize($item['ximaiimg']);
        }
        unset($item);
        return [
            'data' => $res['data'],
            'total' => $res['total']
        ];
    }

    public function searchCheck($param)
    {
        $conn = DB::table("ssy_processing_list as p")->join("ssy_productlist_bangdin_new as n", 'p.sku_code', '=', 'n.sku_code')
            ->select(["p.id","p.processing_code", "p.sku_code", "p.factory_name", "p.type_no", "p.type_name", "p.order_processor", "p.num", "n.id as n_id",
               "n.fabric", "n.m_status", "n.lining_one", "n.lining_two", "n.lining_three", "n.lining_four", "n.ximainame", "n.ximaiimg", "n.ban", "n.lining_five"]);
        if($param['processing_code'] ?? ""){
            $conn = $conn->where('p.processing_code', $param['processing_code']);
        }
        if($param['sku_code'] ?? ""){
            $conn = $conn->where('p.sku_code', 'like', $param['sku_code']."%");
        }
        if($param['factory_name'] ?? ""){
            $conn = $conn->where('p.factory_name',  $param['factory_name']);
        }
        if(isset($param['m_status']) && $param['m_status'] != ""){
            $conn = $conn->where('n.m_status', $param['m_status']);
        }
        $res = $conn->orderBy('p.createtime', 'desc')->paginate((Int)$param['limit'])->toArray();
        return [
            'data' => $res['data'],
            'total' => $res['total']
        ];
    }

    public function del($id)
    {
        return (new SsyProductlistBangdinNewTable())->deleteData($id);
    }

    public function getSku($typeNo)
    {
        $list = (new SsyProcessingListTable())->where('type_no', $typeNo)->groupBy(["sku_code", "type_no"])->get(['sku_code', 'type_no'])->toArray();
        return array_column($list, 'sku_code');
    }

    public function save($data)
    {
        $data = json_decode($data, true);
        $id = $data['id'] ?? "";
        $fabricList = ['lining_one', 'lining_two', 'lining_three', 'lining_four', 'lining_five'];
        $fabric = "";
        foreach ($fabricList as $column){
            if(empty($data[$column])){
                break;
            }
            $fabric .= $data[$column]."\r\n";
        }
        if(empty($fabric)){
            return ['code' => -1, 'msg' => '请至少填一个面辅料'];
        }
        $fabric .= "(装饰部分除外)";
        //编辑
        if(!empty($id)){
            $saveData = [
                'fabric'=>$fabric,
                'lining_one'=>$data['lining_one'],
                'lining_two'=>$data['lining_two'],
                'lining_three'=>$data['lining_three'],
                'lining_four'=>$data['lining_four'],
                'lining_five'=>$data['lining_five'],
                'ximainame'=>$data['ximainame'],
                'ximaiimg'=>serialize($data['ximaiimg']),
                'ximaiid'=>$data['ximaiid'],
                'ban'=>$data['ban'] ?? "",
            ];
            return (new SsyProductlistBangdinNewTable())->updateData($id, $saveData);
        }
        //新增
        if(empty($data['sku_code'])){
            return ['code' => -1, 'msg' => '未选择sku'];
        }
        $exist = (new SsyProductlistBangdinNewTable())->whereIn('sku_code', $data['sku_code'])->exists();
        if($exist){
            return ['code' => -1, 'msg' => '添加的sku里面, 已经存在面料信息'];
        }
        DB::beginTransaction();
        foreach($data['sku_code'] as $skuCode){
            $saveData = [
                'type_no' => $data['type_no'],
                'sku_code' => $skuCode,
                'fabric'=>$fabric,
                'lining_one'=>$data['lining_one'],
                'lining_two'=>$data['lining_two'],
                'lining_three'=>$data['lining_three'],
                'lining_four'=>$data['lining_four'],
                'lining_five'=>$data['lining_five'],
                'ximainame'=>$data['ximainame'],
                'ximaiimg'=>serialize($data['ximaiimg']),
                'ximaiid'=>$data['ximaiid'],
                'ban'=>$data['ban'] ?? "",
            ];
            $res = (new SsyProductlistBangdinNewTable())->createData($saveData);
            if($res['code'] !== 0){
                DB::rollBack();
                return $res;
            }
        }
        DB::commit();
        return ['code' => 0];
    }

    public function printInfo($typeNo)
    {
        $field = ["processing_code","number","type_no","sku_code","color_code","size_code","factory_name","number"];
        $processingDetailObj = (new SsyProcessingListDetailsTable())->where('sku_code', 'like', $typeNo."%")->first($field)->toArray();
        $fabricObj = (new SsyProductlistBangdinNewTable())->where('sku_code', $processingDetailObj['sku_code'])->first(['fabric', 'ximaiimg']);
        $processingDetailObj['fabric'] = $fabricObj['fabric'];
        $processingDetailObj['ximaiimg'] = !empty($fabricObj['ximaiimg']) ? unserialize($fabricObj['ximaiimg']) : [];
        return $processingDetailObj;
    }

    public function checkStatus($ids, $status)
    {
        $ids = json_decode($ids, true);
        return (new SsyProductlistBangdinNewTable())->updateData($ids, ['m_status' => (Int)$status, 'confirm_time' => date("Y-m-d H:i:s")]);
    }
}
